{% extends 'encyclopedia/base.html' %}

{% block title %}相机列表 - 相机百科{% endblock %}

{% block content %}
    <div class="page-header">
        <h1><i class="fa fa-camera"></i> 相机列表</h1>
        <p class="text-muted">发现最新、最全面的相机信息</p>
    </div>

    <!-- 搜索和筛选区域 -->
    <div class="card mb-6 shadow-sm">
        <div class="card-body">
            <form method="get" class="row g-3 align-items-end">
                <div class="col-md-4">
                    <label for="category" class="form-label">分类筛选</label>
                    <select name="category" id="category" class="form-select rounded-lg border-gray-200">
                        <option value="">所有分类</option>
                        {% for category in categories %}
                            <option value="{{ category.id }}" {% if request.GET.category == category.id|stringformat:"s" %}selected{% endif %}>
                                {{ category.name }}
                            </option>
                        {% endfor %}
                    </select>
                </div>
                
                <div class="col-md-6">
                    <label for="search" class="form-label">搜索相机</label>
                    <div class="input-group">
                        <span class="input-group-text rounded-l-lg bg-gray-50 border-gray-200">
                            <i class="fa fa-search text-gray-400"></i>
                        </span>
                        <input 
                            type="text" 
                            id="search"
                            name="search" 
                            class="form-control rounded-r-lg border-gray-200 focus:ring-2 focus:ring-primary/30 focus:border-primary"
                            placeholder="输入相机名称、品牌或关键词..." 
                            value="{{ request.GET.search }}"
                        >
                    </div>
                </div>
                
                <div class="col-md-2">
                    <button class="btn btn-primary w-100 rounded-lg shadow-sm hover:shadow" type="submit">
                        <i class="fa fa-filter mr-2"></i> 筛选
                    </button>
                </div>
            </form>
            
            <!-- 筛选结果信息 -->
            {% if request.GET.category or request.GET.search %}
                <div class="mt-3 text-sm text-gray-600">
                    <i class="fa fa-info-circle mr-1"></i> 筛选结果：共找到 <strong>{{ cameras.paginator.count }}</strong> 台相机
                    <a href="{% url 'encyclopedia:camera_list' %}" class="ml-2 text-primary hover:underline">
                        清除筛选 <i class="fa fa-times"></i>
                    </a>
                </div>
            {% endif %}
        </div>
    </div>

    <!-- 相机卡片网格 -->
    <div class="row row-cols-1 row-cols-md-2 row-cols-lg-3 g-4">
        {% for camera in cameras %}
            <div class="col">
                <div class="card h-100 overflow-hidden group">
                    <!-- 相机卡片头部 -->
                    <div class="relative bg-gradient-to-r from-primary/90 to-primary p-4 text-white">
                        <span class="absolute top-3 right-3 bg-white/20 text-xs font-medium py-1 px-2 rounded-full backdrop-blur-sm">
                            {{ camera.category.name }}
                        </span>
                        <h5 class="card-title text-xl font-bold mb-1 group-hover:text-secondary transition-colors duration-300">
                            {{ camera.title }}
                        </h5>
                        <div class="flex items-center text-white/90">
                            <i class="fa fa-building-o mr-2"></i>
                            <span>{{ camera.brand }}</span>
                        </div>
                    </div>
                    
                    <!-- 相机卡片内容 -->
                    <div class="card-body">
                        <div class="mb-3">
                            <span class="inline-block bg-light rounded-full px-3 py-1 text-xs font-semibold text-gray-700 mr-2 mb-2">
                                <i class="fa fa-calendar-o mr-1"></i> {{ camera.created_at|date:"Y年m月" }}
                            </span>
                        </div>
                        
                        <p class="card-text text-gray-600 mb-4 line-clamp-3">
                            {{ camera.content|truncatechars:120 }}
                        </p>
                        
                        <!-- 相机卡片操作按钮 -->
                        <div class="d-flex gap-2">
                            <a 
                                href="{% url 'encyclopedia:camera_detail' camera.id %}" 
                                class="btn btn-primary flex-1 group-hover:shadow-md transition-all duration-300"
                            >
                                <i class="fa fa-eye mr-2"></i> 查看详情
                            </a>
                            <a 
                                href="{% url 'encyclopedia:camera_edit' camera.id %}" 
                                class="btn btn-outline-secondary group-hover:bg-secondary group-hover:text-white transition-all duration-300"
                            >
                                <i class="fa fa-pencil"></i>
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        {% empty %}
            <!-- 空状态 -->
            <div class="col-12">
                <div class="bg-white rounded-xl shadow-sm p-8 text-center">
                    <div class="inline-flex items-center justify-center w-16 h-16 bg-gray-100 rounded-full mb-4">
                        <i class="fa fa-search text-gray-400 text-2xl"></i>
                    </div>
                    <h3 class="text-lg font-medium text-gray-900 mb-2">没有找到符合条件的相机</h3>
                    <p class="text-gray-500 mb-6">尝试调整筛选条件或搜索关键词</p>
                    <a 
                        href="{% url 'encyclopedia:camera_list' %}" 
                        class="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-primary hover:bg-primary/90"
                    >
                        <i class="fa fa-refresh mr-2"></i> 重置筛选
                    </a>
                </div>
            </div>
        {% endfor %}
    </div>

    <!-- 分页控件 -->
    {% if is_paginated %}
        <nav aria-label="分页导航" class="mt-8">
            <ul class="pagination justify-content-center">
                {% if page_obj.has_previous %}
                    <li class="page-item">
                        <a 
                            class="page-link rounded-lg border-gray-200 text-gray-700 hover:bg-primary/5 hover:text-primary transition-colors"
                            href="?page={{ page_obj.previous_page_number }}{% if request.GET.category %}&category={{ request.GET.category }}{% endif %}{% if request.GET.search %}&search={{ request.GET.search }}{% endif %}"
                            aria-label="上一页"
                        >
                            <span aria-hidden="true"><i class="fa fa-chevron-left"></i></span>
                        </a>
                    </li>
                {% else %}
                    <li class="page-item disabled">
                        <a 
                            class="page-link rounded-lg border-gray-200 bg-gray-50 text-gray-400 cursor-not-allowed"
                            href="#" 
                            tabindex="-1" 
                            aria-disabled="true"
                            aria-label="上一页"
                        >
                            <span aria-hidden="true"><i class="fa fa-chevron-left"></i></span>
                        </a>
                    </li>
                {% endif %}

                {% for num in page_obj.paginator.page_range %}
                    {% if page_obj.number == num %}
                        <li class="page-item active" aria-current="page">
                            <a 
                                class="page-link rounded-lg bg-primary border-primary text-white hover:bg-primary/90 hover:border-primary/90"
                                href="#"
                            >
                                {{ num }}
                            </a>
                        </li>
                    {% else %}
                        <li class="page-item">
                            <a 
                                class="page-link rounded-lg border-gray-200 text-gray-700 hover:bg-primary/5 hover:text-primary transition-colors"
                                href="?page={{ num }}{% if request.GET.category %}&category={{ request.GET.category }}{% endif %}{% if request.GET.search %}&search={{ request.GET.search }}{% endif %}"
                            >
                                {{ num }}
                            </a>
                        </li>
                    {% endif %}
                {% endfor %}

                {% if page_obj.has_next %}
                    <li class="page-item">
                        <a 
                            class="page-link rounded-lg border-gray-200 text-gray-700 hover:bg-primary/5 hover:text-primary transition-colors"
                            href="?page={{ page_obj.next_page_number }}{% if request.GET.category %}&category={{ request.GET.category }}{% endif %}{% if request.GET.search %}&search={{ request.GET.search }}{% endif %}"
                            aria-label="下一页"
                        >
                            <span aria-hidden="true"><i class="fa fa-chevron-right"></i></span>
                        </a>
                    </li>
                {% else %}
                    <li class="page-item disabled">
                        <a 
                            class="page-link rounded-lg border-gray-200 bg-gray-50 text-gray-400 cursor-not-allowed"
                            href="#" 
                            tabindex="-1" 
                            aria-disabled="true"
                            aria-label="下一页"
                        >
                            <span aria-hidden="true"><i class="fa fa-chevron-right"></i></span>
                        </a>
                    </li>
                {% endif %}
            </ul>
        </nav>
    {% endif %}

    <script>
        // 添加卡片加载动画效果
        document.addEventListener('DOMContentLoaded', function() {
            const cards = document.querySelectorAll('.card');
            cards.forEach((card, index) => {
                setTimeout(() => {
                    card.style.opacity = '1';
                }, 100 * index);
            });
        });
    </script>
{% endblock %}